package org.tinaelle.client;

import org.tinaelle.client.utils.DefaultAsyncCallback;
import org.tinaelle.client.utils.ScreenManager;
import org.tinaelle.server.service.AdI18NServiceAsync;
import org.tinaelle.server.service.AdParameterServiceAsync;
import org.tinaelle.server.service.AdSecurityServiceAsync;
import org.tinaelle.server.service.AdSequenceServiceAsync;
import org.tinaelle.server.service.AdSystemServiceAsync;
import org.tinaelle.server.service.AdTreeServiceAsync;
import org.tinaelle.server.service.AdWindowServiceAsync;
import org.tinaelle.server.service.TinaelleServiceAsync;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.HistoryListener;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.PopupPanel;
import com.gwtext.client.widgets.MessageBox;

public class TinaelleEntryPoint implements EntryPoint, HistoryListener {
	public static PopupPanel	messagePanel	= new PopupPanel( true );
	protected ScreenManager	 screenManager;

	public void onModuleLoad() {
		MessageBox.wait( "Please Wait", "Initialization" );

		// Add history listener
		History.addHistoryListener( this );

		setup();

	}

	public void afterSetup() {
		MessageBox.confirm( "Setup finish", "Setup is finished, enjoy..." );
	}

	public void setup() {
		disableAudit();
	}

	protected void disableAudit() {
		TinaelleServiceAsync.Util.getInstance().disableAudit( new DefaultAsyncCallback<Void>() {
			public void onSuccess(final Void result) {
				fillInitialValuesSequences();
			}
		} );
	}

	protected void fillInitialValuesSequences() {
		MessageBox.updateText( "Sequences Initialization" );
		AdSequenceServiceAsync.Util.getInstance().fillInitialValues( new DefaultAsyncCallback<Void>() {
			public void onSuccess(final Void result) {
				fillInitialValuesForEntityType();
			} // end onSuccess()
		} );
	}

	protected void fillInitialValuesForEntityType() {
		MessageBox.updateText( "System Initialization : Entity Types" );
		AdSystemServiceAsync.Util.getInstance().fillInitialValuesForEntityType( new DefaultAsyncCallback<Void>() {
			public void onSuccess(final Void result) {
				fillInitialValuesForTable();
			} // end onSuccess()
		} );
	}

	protected void fillInitialValuesForTable() {
		MessageBox.updateText( "System Initialization : Tables" );
		AdSystemServiceAsync.Util.getInstance().fillInitialValuesForTable( new DefaultAsyncCallback<Void>() {
			public void onSuccess(final Void result) {
				fillInitialValuesForReference();
			} // end onSuccess()
		} );
	}

	protected void fillInitialValuesForReference() {
		MessageBox.updateText( "System Initialization : References" );
		AdSystemServiceAsync.Util.getInstance().fillInitialValuesForReference( new DefaultAsyncCallback<Void>() {
			public void onSuccess(final Void result) {
				fillInitialValuesForReferenceList();
			} // end onSuccess()
		} );
	}

	protected void fillInitialValuesForReferenceList() {
		MessageBox.updateText( "System Initialization : Reference Lists" );
		AdSystemServiceAsync.Util.getInstance().fillInitialValuesForReferenceList( new DefaultAsyncCallback<Void>() {
			public void onSuccess(final Void result) {
				fillInitialValuesForReferenceTable();
			} // end onSuccess()
		} );
	}

	protected void fillInitialValuesForReferenceTable() {
		MessageBox.updateText( "System Initialization : Reference Tables" );
		AdSystemServiceAsync.Util.getInstance().fillInitialValuesForReferenceTable( new DefaultAsyncCallback<Void>() {
			public void onSuccess(final Void result) {
				fillInitialValuesForColumn();
			} // end onSuccess()
		} );
	}

	protected void fillInitialValuesForColumn() {
		MessageBox.updateText( "System Initialization : Columns" );
		AdSystemServiceAsync.Util.getInstance().fillInitialValuesForColumn( new DefaultAsyncCallback<Void>() {
			public void onSuccess(final Void result) {
				fillInitialValuesForParameter();
			} // end onSuccess()
		} );
	}

	protected void fillInitialValuesForParameter() {
		MessageBox.updateText( "Parameters Initialization : Parameters" );
		AdParameterServiceAsync.Util.getInstance().fillInitialValuesForParameter( new DefaultAsyncCallback<Void>() {
			public void onSuccess(final Void result) {
				fillInitialValuesForParameterValue();
			} // end onSuccess()
		} );
	}

	protected void fillInitialValuesForParameterValue() {
		MessageBox.updateText( "Parameters Initialization : Parameters Values" );
		AdParameterServiceAsync.Util.getInstance().fillInitialValuesForParameterValue(
		        new DefaultAsyncCallback<Void>() {
			        public void onSuccess(final Void result) {
				        fillInitialValuesForUser();
			        } // end onSuccess()
		        } );
	}

	protected void fillInitialValuesForUser() {
		MessageBox.updateText( "Security Initialization : Users" );
		AdSecurityServiceAsync.Util.getInstance().fillInitialValuesForUser( new DefaultAsyncCallback<Void>() {
			public void onSuccess(final Void result) {
				fillInitialValuesForGroup();
			} // end onSuccess()
		} );
	}

	protected void fillInitialValuesForGroup() {
		MessageBox.updateText( "Security Initialization : Groups" );
		AdSecurityServiceAsync.Util.getInstance().fillInitialValuesForGroup( new DefaultAsyncCallback<Void>() {
			public void onSuccess(final Void result) {
				fillInitialValuesForScreen();
			} // end onSuccess()
		} );
	}

	protected void fillInitialValuesForScreen() {
		MessageBox.updateText( "Window Initialization : Screens" );
		AdWindowServiceAsync.Util.getInstance().fillInitialValuesForScreen( new DefaultAsyncCallback<Void>() {
			public void onSuccess(final Void result) {
				fillInitialValuesForTabs();
			} // end onSuccess()
		} );
	}

	protected void fillInitialValuesForTabs() {
		MessageBox.updateText( "Window Initialization : Tabs" );
		AdWindowServiceAsync.Util.getInstance().fillInitialValuesForTab( new DefaultAsyncCallback<Void>() {
			public void onSuccess(final Void result) {
				fillInitialValuesForFields();
			} // end onSuccess()
		} );
	}

	protected void fillInitialValuesForFields() {
		MessageBox.updateText( "Window Initialization : Fields" );
		AdWindowServiceAsync.Util.getInstance().fillInitialValuesForField( new DefaultAsyncCallback<Void>() {
			public void onSuccess(final Void result) {
				fillInitialValuesForRole();
			} // end onSuccess()
		} );
	}

	protected void fillInitialValuesForRole() {
		MessageBox.updateText( "Security Initialization : Roles" );
		AdSecurityServiceAsync.Util.getInstance().fillInitialValuesForRole( new DefaultAsyncCallback<Void>() {
			public void onSuccess(final Void result) {
				fillInitialValuesForLanguage();
			} // end onSuccess()
		} );
	}

	protected void fillInitialValuesForLanguage() {
		MessageBox.updateText( "I18N Initialization : Languages" );
		AdI18NServiceAsync.Util.getInstance().fillInitialValuesForLanguage( new DefaultAsyncCallback<Void>() {
			public void onSuccess(final Void result) {
				fillInitialValuesForCurrency();
			} // end onSuccess()
		} );
	}

	protected void fillInitialValuesForCurrency() {
		MessageBox.updateText( "I18N Initialization : Currencies" );
		AdI18NServiceAsync.Util.getInstance().fillInitialValuesForCurrency( new DefaultAsyncCallback<Void>() {
			public void onSuccess(final Void result) {
				fillInitialValuesForCountry();
			} // end onSuccess()
		} );
	}

	protected void fillInitialValuesForCountry() {
		MessageBox.updateText( "I18N Initialization : Countries" );
		AdI18NServiceAsync.Util.getInstance().fillInitialValuesForCountry( new DefaultAsyncCallback<Void>() {
			public void onSuccess(final Void result) {
				fillInitialValuesTree();
			} // end onSuccess()
		} );
	}

	protected void fillInitialValuesTree() {
		MessageBox.updateText( "Tree Initialization : Trees" );
		AdTreeServiceAsync.Util.getInstance().fillInitialValuesForTree( new DefaultAsyncCallback<Void>() {
			public void onSuccess(final Void result) {
				fillInitialValuesTreeNode();
			} // end onSuccess()
		} );
	}

	protected void fillInitialValuesTreeNode() {
		MessageBox.updateText( "Tree Initialization : Tree Nodes" );
		AdTreeServiceAsync.Util.getInstance().fillInitialValuesForTreeNode( new DefaultAsyncCallback<Void>() {
			public void onSuccess(final Void result) {
				enableAudit();
			} // end onSuccess()
		} );
	}

	protected void enableAudit() {
		TinaelleServiceAsync.Util.getInstance().enableAudit( new DefaultAsyncCallback<Void>() {
			public void onSuccess(final Void result) {
				MessageBox.hide();
				afterSetup();
			}
		} );
	}

	/**
	 * DOCUMENT ME!
	 * 
	 * @param historyToken
	 *            DOCUMENT ME!
	 */
	public void onHistoryChanged(final String historyToken) {
		screenManager.showScreen( historyToken );
	} // end onHistoryChanged()

	/**
	 * DOCUMENT ME!
	 * 
	 * @param title
	 *            DOCUMENT ME!
	 * @param message
	 *            DOCUMENT ME!
	 */
	public static void showMessage(final String title, final String message) {
		messagePanel.setPopupPosition( 500, 300 );
		messagePanel.setWidget( new HTML( getMessageHtml( title, message ) ) );
		messagePanel.setWidth( "300px" );
		messagePanel.show();
	} // end showMessage()

	private static native String getMessageHtml(String title, String message) /*-{
	      return ['<div class="msg">',
	              '<div class="x-box-tl"><div class="x-box-tr"><div class="x-box-tc"></div></div></div>',
	              '<div class="x-box-ml"><div class="x-box-mr"><div class="x-box-mc"><h3>', title, '</h3>', message, '</div></div></div>',
	              '<div class="x-box-bl"><div class="x-box-br"><div class="x-box-bc"></div></div></div>',
	              '</div>'].join('');
	      }-*/;
}
